package top.wintp.juhedatademo.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import top.wintp.juhedatademo.R;
import top.wintp.juhedatademo.adapter.HomePagerAdapter;
import top.wintp.juhedatademo.fragment.BaseFragment;
import top.wintp.juhedatademo.fragment.impl.CaiJingFragment;
import top.wintp.juhedatademo.fragment.impl.GuoJiFragment;
import top.wintp.juhedatademo.fragment.impl.GuoNeiFragment;
import top.wintp.juhedatademo.fragment.impl.JunShiFragment;
import top.wintp.juhedatademo.fragment.impl.KejiFragment;
import top.wintp.juhedatademo.fragment.impl.SheHuiFragment;
import top.wintp.juhedatademo.fragment.impl.ShiShangFragment;
import top.wintp.juhedatademo.fragment.impl.TiYuFragment;
import top.wintp.juhedatademo.fragment.impl.TopFragment;
import top.wintp.juhedatademo.fragment.impl.YuLeFragment;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTab_pic;
    private ViewPager mVp_content;
    private List<BaseFragment> mBaseFragmentList;
    private FragmentManager mFm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        mTab_pic = findViewById(R.id.tab_pic);
        mVp_content = findViewById(R.id.vp_content);

        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mBaseFragmentList = new ArrayList<>();
        String[] homeTopicTitleArray = getResources().getStringArray(R.array.homeTopicTitle);
//类型,,top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐)
        // ,tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
        mBaseFragmentList.add(BaseFragment.newInstance(TopFragment.class, homeTopicTitleArray[0]));
        mBaseFragmentList.add(BaseFragment.newInstance(SheHuiFragment.class, homeTopicTitleArray[1]));
        mBaseFragmentList.add(BaseFragment.newInstance(GuoNeiFragment.class, homeTopicTitleArray[2]));
        mBaseFragmentList.add(BaseFragment.newInstance(GuoJiFragment.class, homeTopicTitleArray[3]));
        mBaseFragmentList.add(BaseFragment.newInstance(YuLeFragment.class, homeTopicTitleArray[4]));
        mBaseFragmentList.add(BaseFragment.newInstance(TiYuFragment.class, homeTopicTitleArray[5]));
        mBaseFragmentList.add(BaseFragment.newInstance(JunShiFragment.class, homeTopicTitleArray[6]));
        mBaseFragmentList.add(BaseFragment.newInstance(KejiFragment.class, homeTopicTitleArray[7]));
        mBaseFragmentList.add(BaseFragment.newInstance(CaiJingFragment.class, homeTopicTitleArray[8]));
        mBaseFragmentList.add(BaseFragment.newInstance(ShiShangFragment.class, homeTopicTitleArray[9]));

        mFm = getSupportFragmentManager();

        HomePagerAdapter homePagerAdapter = new HomePagerAdapter(mFm, mBaseFragmentList);


        mVp_content.setAdapter(homePagerAdapter);
        mTab_pic.setupWithViewPager(mVp_content);
    }
}
